# SPDX-License-Identifier: Apache-2.0
BMV2_SWITCH_EXE = simple_switch_grpc

BUILD_DIR = build
PCAP_DIR = pcaps
LOG_DIR = logs

P4C = p4c-bm2-ss
P4C_ARGS = --p4runtime-files $(BUILD_DIR)/advanced_tunnel.p4info.txt

RUN_SCRIPT = ../../utils/run_exercise.py

TOPO = topology.json

source = advanced_tunnel.p4
compiled_json = $(BUILD_DIR)/advanced_tunnel.json

all: run

run: build
	sudo python3 $(RUN_SCRIPT) -t $(TOPO) -j $(compiled_json) -b $(BMV2_SWITCH_EXE)

stop:
	sudo mn -c

build: dirs $(compiled_json)

$(compiled_json): $(source)
	$(P4C) --p4v 16 $(P4C_ARGS) -o $@ $<

dirs:
	mkdir -p $(BUILD_DIR) $(PCAP_DIR) $(LOG_DIR)

clean: stop
	rm -f *.pcap
	rm -rf $(BUILD_DIR) $(PCAP_DIR) $(LOG_DIR)

# 手动编译目标
manual:
	mkdir -p $(BUILD_DIR) $(PCAP_DIR) $(LOG_DIR)
	$(P4C) --p4v 16 $(P4C_ARGS) -o $(compiled_json) $(source)
